<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 地址接口
 */
class Useraddress extends Api
{
    protected $noNeedLogin = ['getProvince', 'getCity', 'getCounty'];
    protected $noNeedRight = ['*'];

    /**
     * 省份
     */
    public function getProvince()
    {
        $list = db('province')->select();
        $this->success('success', $list);
    }

    /**
     * 城市
     */
    public function getCity()
    {
        $pid = $this->request->request('pid');
        if (!$pid) {
            $this->error('参数错误');
        }
        $list = db('city')->field('id, name')->where('province_id', $pid)->select();
        $this->success('success', $list);
    }

    /**
     * 区县
     */
    public function getCounty()
    {
        $cid = $this->request->request('cid');
        if (!$cid) {
            $this->error('参数错误');
        }
        $default[] = ['id' => 1, 'name'  => '市区'];
        $list = db('county')->field('id, name')->where('city_id', $cid)->select();
        $data = array_merge($default, $list);
        $this->success('success', $data);
    }

    /**
     * 添加地址
     */
    public function add()
    {
        $data['name'] = $this->request->request('name');
        $data['detail'] = $this->request->request('detail');
        $data['province_id'] = $this->request->request('pid');
        $data['city_id'] = $this->request->request('cid');
        $data['county_id'] = $this->request->request('ccid');
        $data['default'] = $this->request->request('default', 0);
        $data['mobile'] = $this->request->request('mobile');

        $validate = validate('\app\admin\validate\user\Address');
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        $userInfo = $this->auth->getUserinfo();
        $data['user_id'] = $userInfo['id'];
		//$data['user_id'] = 25;
        if ($data['default'] == 1) {
            db('user_address')->where('user_id', $data['user_id'])->update(['default' => 0]);
        }
        $data['createtime'] = time();
        $res = db('user_address')->insertGetId($data);

        if ($res) {
            $this->success('success');
        } else {
            $this->error('db error');
        }

    }

    /**
     * 修改地址
     */
    public function edit()
    {
        $id = $this->request->post('id', 0);
        $data['name'] = $this->request->request('name');
        $data['detail'] = $this->request->request('detail');
        $data['province_id'] = $this->request->request('pid');
        $data['city_id'] = $this->request->request('cid');
        $data['county_id'] = $this->request->request('ccid');
        $data['default'] = $this->request->request('default', 0);
        $data['mobile'] = $this->request->request('mobile');

        $userInfo = $this->auth->getUserinfo();
        $data['user_id'] = $userInfo['id'];
        if (!$id) {
            $this->add();
        }

        $info = db('user_address')->where('user_id', $userInfo['id'])->where('id', $id)->find();
        if (!$info) {
            $this->error('没有相关信息');
        }

        $validate = validate('\app\admin\validate\user\Address');
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }


        if ($data['default'] == 1) {
            db('user_address')->where('user_id', $data['user_id'])->update(['default' => 0]);
        }
        $res = db('user_address')->where('id', $id)->update($data);

        if ($res !== false) {
            $this->success('success');
        } else {
            $this->error('db error');
        }
    }

    /**
     * 设置默认地址
     */
    public function setDefault()
    {
        $id = $this->request->post('id');
        $userInfo = $this->auth->getUserinfo();

        if (!$id) {
            $this->error('缺少必要参数');
        }

        $info = db('user_address')->where('user_id', $userInfo['id'])->where('id', $id)->find();
        if (!$info) {
            $this->error('没有相关信息');
        }
        if ($info['default'] == 1) {
            $this->success('success');
        } 
        db('user_address')->where('user_id', $userInfo['id'])->update(['default' => 0]);
        db('user_address')->where('id', $id)->update(['default' => 1]);
        $this->success('success');
    } 

    /**
     * 删除地址
     */
    public function delete()
    {
        $id = $this->request->post('id');
        $userInfo = $this->auth->getUserinfo();

        if (!$id) {
            $this->error('缺少必要参数');
        }

        $info = db('user_address')->where('user_id', $userInfo['id'])->where('id', $id)->find();
        if (!$info) {
            $this->error('没有相关信息');
        }

        db('user_address')->where('id', $id)->delete();
        $this->success('success');
    }

    /**
     * 获取地址列表
     */
    public function getList()
    {
        $limit = $this->request->request('limit', 10);
        $userInfo = $this->auth->getUserinfo();
		//$userInfo['id'] = 25;
        $list = db('user_address')->where('user_id', $userInfo['id'])
                                  ->paginate($limit);
								  $lists=[];
		foreach($list as $o=>$v){
			$lists[]=array(
				'createtime'=>$v['createtime'],
				'default'=>$v['default'],
				'detail'=>$v['detail'],
				'id'=>$v['id'],
				'mobile'=>$v['mobile'],
				'name'=>$v['name'],
				'status'=>$v['status'],
				'user_id'=>$v['user_id'],
				'city_id'=>$v['city_id'],
				'city_name'=>db('city')->field('id, name')->where('id', $v['city_id'])->find()['name'],
				'county_id'=>$v['county_id'],
				'county_name'=>db('county')->field('id, name')->where('id', $v['county_id'])->find()['name'],
				'province_id'=>$v['province_id'],
				'province_name'=>db('province')->field('id, name')->where('id', $v['province_id'])->find()['name'],
			);
		}						  
        $this->success('success', $lists);
    }
}
